package cn.txm.pet.controller;

import cn.txm.basic.util.AjaxResult;
import cn.txm.basic.util.PageList;
import cn.txm.basic.util.RedisUtils;
import cn.txm.org.domain.Employee;
import cn.txm.pet.domain.Adopt;
import cn.txm.pet.domain.Pet;
import cn.txm.pet.query.AdoptQuery;
import cn.txm.pet.query.PetQuery;
import cn.txm.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class PetController {
    @Autowired
    private IPetService petService;
    /**
     * 后动态管理，分页查询宠物寻主消息
     * @param petQuery
     * @return
     */
    @PostMapping("/pet")
    public PageList<Pet> pageList(@RequestBody PetQuery petQuery, HttpServletRequest request){
        Employee employee = RedisUtils.INSTANCE.getUser(request, Employee.class);
        petQuery.setShop_id(employee.getShop().getId());
        return petService.queryPage(petQuery);
    }


    /**
     * 后台管理:宠物批量上架
     * @param ids
     * @return
     */
    @PatchMapping("/petshelves")
    public AjaxResult petshelves(@RequestBody List<Long> ids){
        try {
            petService.petshelves(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*链式编程思想*/
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙！"+e.getMessage());
        }
    }

    /**
     * 后台管理:宠物批量下架
     * @param ids
     * @return
     */
    @PatchMapping("/petnoshelves")
    public AjaxResult petnoshelves(@RequestBody List<Long> ids){
        try {
            petService.petnoshelves(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*链式编程思想*/
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙！"+e.getMessage());
        }
    }
    /**
     * 通过id查询宠物
     * @param id
     * @return
     */
    @GetMapping("/pet/{id}")
    public Pet loadById(@PathVariable("id")Long id){
        return petService.getById(id);
    }
}
